package com.hong.concurrent.chapter10;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author csh
 * @Description 始终已当前线程做为Key值
 * @Date 2018/7/6 22:23
 */
public class ThreadLocalSimulator<T> {
    private final Map<Thread,T> storage = new HashMap<>();

    public void set(T t){
        synchronized (this){
            Thread key = Thread.currentThread();
            storage.put(key,t);
        }
    }

    public T get(){
        synchronized (this){
            Thread key = Thread.currentThread();
            T value = storage.get(key);
            if(null==value){
                return initialValue();

            }
            return  value;
        }
    }

    public T initialValue() {
        return null;
    }
}
